﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace Pavilion.DesignerTool
{
    public class ToolsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public FrameData CurrentFrameData { get; set; }
        public BrushTool BrushTool { get { return brushTool; } set { brushTool = value; OnPropertyChanged("BrushTool"); } }
        public EraserTool EraseTool { get { return eraseTool; } set { eraseTool = value; OnPropertyChanged("EraseTool"); } }
        public int LeftBound { get { return leftBound; } set { leftBound = value; OnPropertyChanged("LeftBound"); } }
        public int RightBound { get { return rightBound; } set { rightBound = value; OnPropertyChanged("RightBound"); } }
        public int UpperBound { get { return upperBound; } set { upperBound = value; OnPropertyChanged("UpperBound"); } }
        public int LowerBound { get { return lowerBound; } set { lowerBound = value; OnPropertyChanged("LowerBound"); } }

        private int leftBound, rightBound, upperBound, lowerBound;
        private BrushTool brushTool;
        private EraserTool eraseTool;

        public void Load()
        {
            BrushTool = new BrushTool();
            EraseTool = new EraserTool();
        }

        public void SetCurrentFrame(FrameData framedata)
        {
            CurrentFrameData = framedata;
            OnPropertyChanged("CurrentFrameData");
        }

        public void SetBrushColor(Color color)
        {
            brushTool.BrushColor = new Microsoft.Xna.Framework.Color(color.R, color.G, color.B);
        }

        public void ApplyCanvasBoundsOnTexture(CTexture2D texture)
        {
            int width = texture.Width + LeftBound + RightBound;
            int height = texture.Height + UpperBound + LowerBound;

            if (width > 0 && height > 0)
                ResizeTexture(texture, LeftBound, RightBound, width, height);

            ResetBounds();
        }

        private void ResizeTexture(CTexture2D texture, int leftBound, int upperBound, int width, int height)
        {
            Render render = new Render();

            render.Begin(width, height);
            DrawManager.Instance.DrawOnScreen(texture.TextureItem,
                new Microsoft.Xna.Framework.Vector2(leftBound, upperBound), new Microsoft.Xna.Framework.Rectangle(0, 0, texture.Width, texture.Height), Microsoft.Xna.Framework.Color.White);
            render.End();

            texture.TextureItem = render.Texture;
        }

        private void ResetBounds()
        {
            LeftBound = 0;
            RightBound = 0;
            UpperBound = 0;
            LowerBound = 0;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
